<?php

class Controller {

    
    public function __construct() {
        global $session, $publication;
        // Check post for actions.
        if( isset($_POST) ) {

            
            if ( isset($_POST['view_user']) && isset($_POST['user_id']) ) {
                header('Location: index.php?page=edit&user=' . $_POST['user_id']); exit;
            }
            
            // We are gathering subs from an advanced search.
            if ( isset($_POST['advanced_search']) ) {
                $search =  array(
                'displayname' => $_POST['displayname'],
                'city' => $_POST['city'],
                'province' => $_POST['province'],
                'country' => $_POST['country']
                );
                    
                $total_pages = 1;   
                $current_tab = 'search';    
                $subscription_set = Subscription::advanced_search($search, $_POST['status']);
                $total_subscriptions = count($subscription_set);
            }
            
 
            
            
            
            
                        
            /**
             * 
             * 
             * 
             * 
             * 
             * 
             * General post actions
             */
            if ( isset($_POST['action']) && !empty($_POST['action'])) {
                
                switch ( $_POST['action']) {
                    
                    case 'login': 
                        if ( isset($_POST['username']) && isset($_POST['password']) ) {
                            if ( $session->login($_POST) ) {
                                header('Location: index.php?page=home'); exit;
                            } 
                        }
                        break;
                        
                    // Reset password
                    case 'reset_pass':
                        if ( isset($_POST['password1']) && isset($_POST['password2']) && $_POST['password1'] == $_POST['password2'] ) {
                            $session->publication->update_password($_POST['password1']);
                        } else {
                            $session->add_get_url("ERROR: Your passwords didn't match.");
                        }
                        break;
            
            
                    case 'scrub':  
                        Subscription::scrub_subs($session->publication->publication_id);
                    break;
                    
                    
                    /**
                     * 
                     * Save an allowance
                     * 
                     */
                    
                    case 'new_allowance':
                        Allowance::add_allowance();
                        break;
                    case 'single_allowance_adjustment':
                        Allowance::adjust_allowance();
                        break;
                    /**
                     * 
                     * Save a pub
                     * 
                     */
                    
                    case 'update_publication':
                        Publication::update_publication();
                        break;
                        
                    /**
                     * 
                     * 
                     * Saves a user.
                     */
                    case 'update_user':
                        User::update_user();
                        break;
                    case 'import_users':
                        User::import_users();
                        break;   
                     /**
                     * 
                     * 
                     * Saves a subscription.
                     */
                    case 'update_subscription':
                       Subscription::update_subscription();
                    break;
                   
                    /**
                     * 
                     * 
                     * 
                     * Generate reports.
                     */
                    case 'report_mailing_labels':
                    case 'report_expiring_expired_labels':
                    case 'report_recently_labels':
                    case 'report_expired_labels':
                    case 'report_weekly_totals':
                    case 'report_active_subs':
                    case 'report_expiring_subs': 
                    case 'report_user':
                    case 'report_export_data':
                        Report::do_report();
                   break;


                }
                
            }
            
            
        }
        
        // Do get actions:
        if ( isset($_GET['action']) ) {
                switch ( $_GET['action']) {
                    // Log out
                    case 'logout': 
                        $session->logout(); 
                        header('Location: index.php?page=home'); exit;
                    break;
                }
        }
        
        // Delete something.
        if ( isset($_GET['delete']) && isset($_GET['id']) ) {
            switch($_GET['delete']) {
                case 'user':
                    // delete the subscription, not the user.
                    Subscription::delete_by_user_id($_GET['id']);
                    break;
                case 'report':
                    Report::delete($_GET['id']);
                    break;
                case 'allowance':
                    Allowance::delete($_GET['id']);
                    break;
                case 'user':
                    Subscription::delete_by_user_id($_GET['id']);
                    break;
            }
        }
        
        
        
        
        self::get_page_template() ;
    }


    public function redirect_to($template, $gets = false) {
        $url = self::page_url( $template );
        $get_string = '';
        if ( is_array($gets) ) {
            foreach($gets as $k => $v ) $get_string .= "&$k=$v";
        } 
        
        
        header("Location: $url$get_string");
        

    }
    
    
    public function get_page_template( $template = '' ) {
        global $session;
        
        
        if ( !empty($template) ) {
            $template_path = TEMPLATE_PATH . 'page.'.$template.'.php';
        } else {
            $page_template = ( !isset($_GET['page']) ) ? 'home' : $_GET['page'];
            $template_path = TEMPLATE_PATH . 'page.'.$page_template.'.php';
        }
        
        if ( file_exists( $template_path ) ) {
            return $template_path;
        } else {
            // 404!
            return TEMPLATE_PATH . 'page.404.php';
        }
        
    }
    
    
    public function page_url( $request = null ) {
        return ( isset($request) ) ? 'index.php?page='.$request : 'index.php?page='.$_GET['page'];
    }
    public function get_page_slug( $request = null ) {
        $default = isset( $_GET['page'] ) ? $_GET['page'] : 'home';
        return ( isset($request) ) ?  $request : $default;
    }
    public function is_page( $request ) {
        return ( $_GET['page'] == $request ) ? true : false; 
    }
    public function get_raw_get_url(){
        if ( count($_GET) ) {
            $url_string = '';
            foreach($_GET as $key => $value ) $url_string .= "&{$key}={$value}";
            return '?' . substr($url_string, 1);
        }
        return false;
    }
    

    
}

$Controller = new Controller;
?>